package yyy.ab.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import yyy.ab.common.core.BaseController;
import yyy.ab.common.core.ResultBean;
import yyy.ab.logging.annotation.Log;
import yyy.ab.modules.system.entity.SysUser;
import yyy.ab.modules.system.service.SysUserService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Api(tags = "系统：用户管理")
@RequestMapping("/system/user")
public class SysUserController extends BaseController {

    private final SysUserService userService;

    /**
     * 用户页面
     */
    @RequestMapping
    public ModelAndView jump() {
        ModelAndView view = new ModelAndView();
        view.setViewName("system/user/index");
        return view;
    }

//    @Log("查询用户")
    @ApiOperation("查询用户")
    @GetMapping("/findPage")
    public Object query(@RequestParam Map<String, Object> params) {
        IPage<SysUser> page = userService.findPage(params);
        return jsonForTable(page);
    }

    @ApiOperation("查询单个用户")
    @GetMapping("/get")
    public SysUser get(Long id) {
        SysUser appModule = userService.getById(id);
        return appModule;
    }

    @ApiOperation("创建用户")
    @PostMapping
    public Object create(@RequestBody SysUser appModule) {
        ResultBean resultBean = userService.create(appModule);
        return resultBean;
    }

//    @Log("修改用户")
    @ApiOperation("修改用户")
    @PutMapping
    public Object update(@RequestBody SysUser appModule) {
        ResultBean resultBean = userService.update(appModule);
        return resultBean;
    }

    @ApiOperation("删除用户")
    @DeleteMapping
    public Object deleteAll(@RequestBody Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        ResultBean resultBean = userService.deleteAll(idList);
        return resultBean;
    }

    @ApiOperation("更新用户状态")
    @GetMapping("/updateStatus")
    public Object updateStatus(Long userId, Integer status) {
        ResultBean resultBean = userService.updateStatus(userId, status);
        return resultBean;
    }

    @ApiOperation("重置用户密码")
    @GetMapping("/resetPassword")
    public Object resetPassword(Long userId) {
        ResultBean resultBean = userService.resetPassword(userId);
        return resultBean;
    }
}
